/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.yuecai.modules.yc_gift_good.service.impl;


import com.yuecai.modules.yc_gift_good.domain.YcGiftGood;
import com.yuecai.modules.yc_gift_good.repository.YcGiftGoodRepository;
import com.yuecai.modules.yc_gift_good.service.YcGiftGoodService;
import com.yuecai.modules.yc_gift_good.service.dto.YcGiftGoodDto;
import com.yuecai.modules.yc_gift_good.service.dto.YcGiftGoodQueryCriteria;
import com.yuecai.modules.yc_gift_good.service.mapstruct.YcGiftGoodMapper;
import com.yuecai.utils.FileUtil;
import com.yuecai.utils.PageUtil;
import com.yuecai.utils.QueryHelp;
import com.yuecai.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author dm
* @date 2020-10-24
**/
@Service
@RequiredArgsConstructor
public class YcGiftGoodServiceImpl implements YcGiftGoodService {

    private final YcGiftGoodRepository ycGiftGoodRepository;
    private final YcGiftGoodMapper ycGiftGoodMapper;

    @Override
    public Map<String,Object> queryAll(YcGiftGoodQueryCriteria criteria, Pageable pageable){
        Page<YcGiftGood> page = ycGiftGoodRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ycGiftGoodMapper::toDto));
    }

    @Override
    public List<YcGiftGoodDto> queryAll(YcGiftGoodQueryCriteria criteria){
        return ycGiftGoodMapper.toDto(ycGiftGoodRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public YcGiftGoodDto findById(Integer id) {
        YcGiftGood ycGiftGood = ycGiftGoodRepository.findById(id).orElseGet(YcGiftGood::new);
        ValidationUtil.isNull(ycGiftGood.getId(),"YcGiftGood","id",id);
        return ycGiftGoodMapper.toDto(ycGiftGood);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public YcGiftGoodDto create(YcGiftGood resources) {
        return ycGiftGoodMapper.toDto(ycGiftGoodRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(YcGiftGood resources) {
        YcGiftGood ycGiftGood = ycGiftGoodRepository.findById(resources.getId()).orElseGet(YcGiftGood::new);
        ValidationUtil.isNull( ycGiftGood.getId(),"YcGiftGood","id",resources.getId());
        ycGiftGood.copy(resources);
        ycGiftGoodRepository.save(ycGiftGood);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            ycGiftGoodRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<YcGiftGoodDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YcGiftGoodDto ycGiftGood : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("订单价格下限", ycGiftGood.getLowerLimit());
            map.put("订单价格上限", ycGiftGood.getUpperLimit());
            map.put("赠送的商品", ycGiftGood.getGoodIds());
            map.put("时间", ycGiftGood.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}